import datetime

import boto3

s3 = boto3.client('s3', region_name="cn-northwest-1", aws_access_key_id="AKIAWTBG3E42IMYODWHL",
                  aws_secret_access_key="onuA0VOzcCvidVPaZGcVESHrLSz2hZ9Qr5gGPG0c")


def ls(bucket_and_path):
    if bucket_and_path.startswith("s3://"):
        bucket_and_path = bucket_and_path.replace('s3://', '')
    parts = bucket_and_path.split('/')
    bucket, prefix = parts[0], '/'.join(parts[1:])
    if not prefix.endswith('/'):
        prefix += '/'
    paginator = s3.get_paginator('list_objects')
    page_iterator = paginator.paginate(Bucket=bucket, Prefix=prefix)
    rs = []
    for response in page_iterator:
        for content in response.get('Contents', []):
            rs.append(content)
    return rs


ac_fault_classif_mapping = ls('s3://dim/ac_fault_classif_mapping')
latest_mapping = datetime.datetime.strptime('2000-01-01 00:00:00', '%Y-%m-%d %H:%M:%S').astimezone()
for j in ac_fault_classif_mapping:
    d = j['LastModified']
    if d > latest_mapping:
        latest_mapping = d
kyd_ods = ls('s3://ods/kyd_ods')
latest_kyd = datetime.datetime.strptime('2000-01-01 00:00:00', '%Y-%m-%d %H:%M:%S').astimezone()
for j in kyd_ods:
    d = j['LastModified']
    if d > latest_mapping:
        latest_kyd = d

if latest_mapping < latest_kyd:
    print("just kyd = 2")


